﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Kanban.Api
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Board",
                url: "Board/{key}",
                defaults: new { controller = "Board", action = "Details" }, 
                constraints: new { key = new GuidConstraint() }
            );

            routes.MapRoute(
                name: "GetTasks",
                url: "Board/{key}/GetTasks",
                defaults: new { controller = "Board", action = "GetTasks" },
                constraints: new { key = new GuidConstraint() }               
            );

            routes.MapRoute(
                name: "AddTask",
                url: "Board/{key}/AddTask",
                defaults: new { controller = "Board", action = "AddTask" },
                constraints: new { key = new GuidConstraint(), httpMthod = new HttpMethodConstraint(new string[]{ "POST" }) }
            );

            routes.MapRoute(
                name: "UpdateTask",
                url: "Board/{key}/UpdateTask",
                defaults: new { controller = "Board", action = "UpdateTask" },
                constraints: new { key = new GuidConstraint(), httpMthod = new HttpMethodConstraint(new string[] { "PUT" }) }
            );

            routes.MapRoute(
                name: "DeleteTask",
                url: "Board/{key}/DeleteTask",
                defaults: new { controller = "Board", action = "DeleteTask" },
                constraints: new { key = new GuidConstraint(), httpMthod = new HttpMethodConstraint(new string[] { "DELETE" }) }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );



        }
    }

    public class GuidConstraint : IRouteConstraint
    {

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (values.ContainsKey(parameterName))
            {
                string stringValue = values[parameterName] as string;

                if (!string.IsNullOrEmpty(stringValue))
                {
                    Guid guidValue;

                    return Guid.TryParse(stringValue, out guidValue) && (guidValue != Guid.Empty);
                }
            }

            return false;
        }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class NoCacheAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();

            base.OnResultExecuting(filterContext);
        }
    }
}